﻿using System;
using System.Collections.Generic;
using System.Linq;

using MonoTouch.SQLite;
using LinkNote.Model;
using Newtonsoft.Json;

namespace LinkNote.Core
{
    public enum CompareType { ModifyTime, CreateTime, Title, Class, Size };

    [Buffer]
    public class LinkNoteFile : BaseModel
    {
        [PrimaryKey, AutoIncrement]
        public override int ID { get; set; }
        public int UserID { get; set; }
        [JsonProperty("catalogueid")]
        public int ClassID { get; set; }

        [JsonProperty("notetitle")]
        public string title { get; set; }
        [JsonProperty("description")]
        public string abstra { get; set; }
        public DateTime createTime { get; set; }
        [JsonProperty("updateTime")]
        public DateTime modifyTime { get; set; }
        public string author { get; set; }
        public string source { get; set; }

        public string tags { get; set; }
        public string revision { get; set; }
        public int noteid { get; set; }
        public int status { get; set; }
        public string md5 { get; set; }
        public int revisioncount { get; set; }
        public int size { get; set; }

        private MainBody mainbody;
        public MainBody Mainbody
        {
            get
            {
                if (mainbody == null)
                    mainbody = new MainBody();
                return mainbody;
            }
        }

        public MainBody MainbodyFromDB
        {
            get
            {
                if(this.noteid==0)
                    mainbody = DBHelper.sqlitedb.Find<MainBody>(m => m.noteid == this.ID);
                else
                    mainbody = DBHelper.sqlitedb.Find<MainBody>(m => m.noteid == this.noteid);

                if (mainbody == null)
                    mainbody = new MainBody();
                return mainbody;
            }
        }

        private List<AttachmentBlock> attachments;
        public List<AttachmentBlock> Attachments
        {
            get
            {
                if(this.noteid==0)
                    attachments = DBHelper.sqlitedb.Table<AttachmentBlock>().Where<AttachmentBlock>(m => m.noteid == this.ID).ToList<AttachmentBlock>();
                else
                    attachments = DBHelper.sqlitedb.Table<AttachmentBlock>().Where<AttachmentBlock>(m => m.noteid == this.noteid).ToList<AttachmentBlock>();
                 return attachments;
           }
        }

        public int CompareByType(CompareType type, LinkNoteFile file, bool desc)
        {
            switch(type)
            {
                case CompareType.ModifyTime:
                    return this.modifyTime.CompareTo(file.modifyTime) * (desc?-1:1); 
                case CompareType.CreateTime:
                    return this.createTime.CompareTo(file.createTime) * (desc ? -1 : 1); 
                case CompareType.Class:
                    return this.ClassID.CompareTo(file.ClassID) * (desc ? -1 : 1); 
                case CompareType.Size:
                    return this.size.CompareTo(file.size) * (desc ? -1 : 1); 
                default:
                    return this.title.CompareTo(file.title) * (desc ? -1 : 1); 
            }
        }
    }
}
